package com.centaline.androidsalesblog.rx.request;

import android.text.TextUtils;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.api.usercenter.AddCollectApi;
import com.centaline.androidsalesblog.api.usercenter.UpdateCollectApi;
import com.centaline.androidsalesblog.constants.AppNameType;
import com.centaline.androidsalesblog.constants.SourceType;
import com.centaline.androidsalesblog.model.responseobject.SimpleUserCenterRo;
import com.centanet.framework.interfaces.IResponseListener;

import rx.Observable;
import rx.Subscriber;

/**
 * 添加收藏
 * Created by guilin on 16/3/21.
 */
final class RequestAddCollectOnSubscribe implements Observable.OnSubscribe<SimpleUserCenterRo> {

    private final Class<?> tag;
    private final AppNameType appNameType;
    private final SourceType sourceType;
    private final String collectId;
    private final String collectValue;
    private final String collectUrl;

    public RequestAddCollectOnSubscribe(Class<?> tag, AppNameType appNameType, SourceType sourceType,
                                        String collectId, String collectValue, String collectUrl) {
        this.tag = tag;
        this.appNameType = appNameType;
        this.sourceType = sourceType;
        this.collectId = collectId;
        this.collectValue = collectValue;
        this.collectUrl = collectUrl;
    }

    @Override
    public void call(final Subscriber<? super SimpleUserCenterRo> subscriber) {
        if (TextUtils.isEmpty(collectId)) {
            AddCollectApi addCollectApi = new AddCollectApi(tag, SimpleUserCenterRo.class, new IResponseListener<SimpleUserCenterRo>() {
                @Override
                public void onResponse(SimpleUserCenterRo simpleUserCenterRo) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(simpleUserCenterRo);
                    }
                    subscriber.onCompleted();
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    subscriber.onError(error);
                }
            });
            addCollectApi.setAppNameType(appNameType);
            addCollectApi.setSourceType(sourceType);
            addCollectApi.setCollectValue(collectValue);
            addCollectApi.setCollectUrl(collectUrl);
            addCollectApi.startRequest();
        } else {
            UpdateCollectApi updateCollectApi = new UpdateCollectApi(tag, SimpleUserCenterRo.class, new IResponseListener<SimpleUserCenterRo>() {
                @Override
                public void onResponse(SimpleUserCenterRo simpleUserCenterRo) {
                    if (!subscriber.isUnsubscribed()) {
                        subscriber.onNext(simpleUserCenterRo);
                    }
                    subscriber.onCompleted();
                }

                @Override
                public void onErrorResponse(VolleyError error) {
                    subscriber.onError(error);
                }
            });
            updateCollectApi.setCollectID(collectId);
            updateCollectApi.setIsDel(0);
            updateCollectApi.startRequest();
        }
    }
}
